﻿using SeeSharpTools.JY.Report;
using Sunny.UI;
using System;
using System.Data;
using System.Drawing;
using System.IO;

namespace SeeSharpTools.Demo.ReportGeneration
{
    public partial class FormExcel : UIPage
    {
        private ExcelReport report;
        private string outputPath;
        private ExcelFont font;
        private DataTable dt = new DataTable();
        private double[] freq = new double[] { 100, 504.0816327, 706.122449, 1110.204082, 1312.244898, 1716.326531, 1918.367347, 2322.44898, 2524.489796, 2928.571429, 3130.612245, 3534.693878, 3736.734694, 4140.816327, 4342.857143, 4746.938776, 4948.979592, 5353.061224, 5555.102041 };
        private double[] mag = new double[] { -0.003843684, -0.061876197, -0.112848283, -0.252606782, -0.351523594, -0.559811744, -0.667703532, -0.940809787, -1.062932608, -1.361288215, -1.509733734, -1.820217546, -1.985364721, -2.32388851, -2.445974611, -2.787932035, -2.992933588, -3.257264758, -3.378190931, };

        public FormExcel()
        {
            InitializeComponent();
        }

        private void FormExcelcs_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void uiButton_Load_Click(object sender, EventArgs e)
        {
            DirectoryInfo directory = new DirectoryInfo(Environment.CurrentDirectory);
            string path = directory.Parent.Parent.FullName + "\\ExampleData\\Template\\JYTek Excel Template.xltx";
            report = new ExcelReport(path);
            report.Show();
            outputPath = Directory.GetParent(Directory.GetParent(path).ToString()) + "\\Report.xlsx";
        }

        private void uiButton_Write_Click(object sender, EventArgs e)
        {
            font = new ExcelFont();
            font.FontColor = Color.Blue;

            DateTime dateTime = DateTime.Now;

            report.WriteTextToReport("Date", dateTime.ToString("yyyy/MM/dd"), font);
            report.WriteTextToReport("Time", dateTime.ToString("HH:mm:ss"), font);
            report.WriteTextToReport("Company", "JYTEK");
            report.WriteTextToReport("Author", "Employee");

            report.WriteTableToReport("Data", dt, true);
            report.InsertGraph("D14", "B15", "C33");
            report.WriteTextToReport("CutOffFrequency", freq[16].ToString());
            report.WriteTextToReport("Loss", mag[16].ToString());
        }

        private void uiButton_Save_Click(object sender, EventArgs e)
        {
            report.SaveAs(outputPath, ExcelSaveFormat.xlsx);
        }

        private void uiButton_Quit_Click(object sender, EventArgs e)
        {
            report.Close();
        }

        private void LoadData()
        {
            //real data we saved in the previous test
            dt.Columns.Add("Frequency(Hz)");
            dt.Columns.Add("Magnitude(dB)");

            for (int i = 0; i < freq.Length; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = freq[i].ToString();
                dr[1] = mag[i].ToString();
                dt.Rows.Add(dr);
            }
            uiDataGridView1.DataSource = dt;
            uiDataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
            easyChartX1.Plot(freq, mag);
        }
    }
}